package com.freemyleft.left.left_app.mian.index.work.waitingforclass;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.freemyleft.left.R2;
import com.freemyleft.left.left_app.R;
import com.freemyleft.left.left_app.mian.index.work.waitingforclass.adapter.SetGridHeight;
import com.freemyleft.left.zapp.delegates.LeftDelegate;


import com.freemyleft.left.zapp.selectimage.BaseDialogUtils;
import com.freemyleft.left.zapp.selectimage.CameraUtils;
import com.freemyleft.left.zapp.selectimage.ImageSelectUtils;
import com.freemyleft.left.zapp.selectimage.PicassoUtils;
import com.freemyleft.left.zapp.selectimage.activity.ImageShowActivity;
import com.freemyleft.left.zapp.selectimage.bean.Image;
import com.freemyleft.left.zapp.selectimage.bean.UpdateImageBean;
import com.freemyleft.left.zapp.selectimage.imageload.ImageLoad;
import com.freemyleft.left.zapp.selectimage.listener.CameraListener;
import com.freemyleft.left.zapp.selectimage.listener.SelectImageFinishListener;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class HomeworkDelegate extends LeftDelegate {
    @BindView(R2.id.m_grid)
    GridView mGrid;
    private List<UpdateImageBean> mImageList = new ArrayList<>();
    private int mMax = 40;
    private Adapter mAdapter = null;
    private List<String> mUrlImagesList = new ArrayList<>();
    private UpdateImageBean updateImageBean;


    private void initGrid() {
        mAdapter = new Adapter();
        mGrid.setAdapter(mAdapter);
        SetGridHeight.setGvHeight(mGrid, mAdapter, 3);

    }

    private void initData() {


    }


    private void updateImage(Image image) {
//        RestClient.builder()
//                .url("/upload/imagesForAPP.do")
//                .loader(mActivity)
//                .file(image.path)
//                .success(new ISuccess() {
//                    @Override
//                    public void onSuccess(String response) {
//                        if (mImageList.size() == mMax)return;
//                        bean = BaseApi.gson.fromJson(response, Phone_updataBean.class);
//                        if (!bean.getRes_status().equals("1")) return;
        mImageList.add(new UpdateImageBean
                (image.path, image.path, image.time, image.width, image.height,image.path));
//                        mAdapter.notifyDataSetChanged();
        SetGridHeight.setGvHeight(mGrid, mAdapter, 3);
    }
//                }).failure(new IFailure() {
//            @Override
//            public void onFailure() {
//                updateImage(image);
//            }
//        }) .buid().upload();

    public static HomeworkDelegate newInstance() {
        
        Bundle args = new Bundle();
        
        HomeworkDelegate fragment = new HomeworkDelegate();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public Object setLayout() {
        return R.layout.delegate_woek_homework;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initGrid();
    }

    public class Adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mImageList.size() == mMax ? mImageList.size() : mImageList.size() + 1;
        }

        @Override
        public Object getItem(int position) {
            return mImageList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(getActivity()).
                    inflate(R.layout.item_select_image, null);
            ImageView imageView = convertView.findViewById(R.id.m_image);
            TextView textView=convertView.findViewById(R.id.tv);
//            imageView.setVisibility(mImageList.size() == mMax && position == mMax ? View.GONE : View.VISIBLE);
            if (mImageList.size() != position) {
                if (mImageList.get(position).isNetImage) {//网络图片
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    ImageLoad.loadImgDefault(getActivity(), imageView, mImageList.get(position).url);
                } else {//本地图片
                    PicassoUtils.picasso(getActivity(), mImageList.get(position).path, 300, 300, imageView);
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

                }
            } else {
                textView.setVisibility(View.GONE);
                imageView.setImageResource(R.mipmap.zhaoxiangji);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

            }
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mImageList.size() == position) {
                        if (mMax - mImageList.size() != 0) {
                            selectImage((mMax - mImageList.size()) / 3 >= 1 ? 3 : (mMax - mImageList.size()) % 4);
                        }
                    } else {
                        Intent intent = new Intent(getActivity(), ImageShowActivity.class);
                        intent.putExtra("images", (Serializable) mImageList);
                        intent.putExtra("position" ,position);
                        startActivity(intent);
                        Toast.makeText(_mActivity, "走eles了", Toast.LENGTH_SHORT).show();
                    }
                }
            });
//            mDelete.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (mUrlImagesList.size() > 0 && mImageList.size() == 1) {
//                        Toast.makeText(_mActivity, "请保留一张图片", Toast.LENGTH_SHORT).show();
//                        return;
//                    }
//                    mImageList.remove(position);
//                    mAdapter.notifyDataSetChanged();
//                    SetGridHeight.setGvHeight(mGrid, mAdapter, 4);
//                }
//            });
            return convertView;
        }
    }

    /**
     * 选择图片
     */
    private void selectImage(final int num) {
        BaseDialogUtils mBaseDialog = new BaseDialogUtils(getActivity(), R.layout.dialog_image_select) {
            @Override
            protected boolean convert(Holder holder) {
                holder.getView(R.id.m_camera).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new CameraUtils(getActivity()).setOnCameraListener(new CameraListener() {

                            @Override
                            public void cameraListener(Image image) {
                                updateImage(image);
                            }
                        }).start();
                        dismiss();
                    }
                });

                holder.getView(R.id.m_photo).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new ImageSelectUtils(getActivity()).setSelectImageNum(num).setOnSelectImageFinishListener(new SelectImageFinishListener() {
                            @Override
                            public void selectImageFinish(List<Image> list) {
                                for (int i = 0; i < list.size(); i++) {
                                    updateImage(list.get(i));
                                }
                            }
                        }).start();
                        dismiss();
                    }
                });
                holder.getView(R.id.m_cancle).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });
                return true;
            }
        };
    }

//    @OnClick(R2.id.img_back)
//    public void onReturn(View view) {
//        getActivity().onBackPressed();
//    }
    //点击上传
//    @OnClick(R2.id.m_ok)
//    public void onOk(View view) {
//        String images = "";
//        for (int i = 0; i < mImageList.size(); i++) {
//            if (mImageList.size() -1 == i){
//                images+= mImageList.get(i).url;
//                continue;
//            }
//            images+= mImageList.get(i).url + ",";
//        }
//
//        EventBus.getDefault().post(new EventBusBean(mEvenBusTag, images));
//
//    }
}
